package com.my.mybasefragment.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.my.mybasefragment.R;
import com.my.mybasefragment.fragment.FragmentTFour;
import com.my.mybasefragment.fragment.FragmentTOne;
import com.my.mybasefragment.fragment.FragmentTThree;
import com.my.mybasefragment.fragment.FragmentTTwo;

public class TransactionActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup rg;
    private RadioButton rbOne;
    private RadioButton rbTwo;
    private RadioButton rbThree;
    private RadioButton rbFour;
    private FragmentTOne fragmentOne;
    private FragmentTTwo fragmentTwo;
    private FragmentTThree fragmentThree;
    private FragmentTFour fragmentFour;
    private FragmentManager fragmentManager;
    private RelativeLayout rlMian;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transaction);
        fragmentManager = getSupportFragmentManager();
        initView();
        setTabSelect(0);
    }

    private void initView() {
        rlMian = findViewById(R.id.rl_main);
        rg = findViewById(R.id.rg_main);
        rbOne = findViewById(R.id.rb_one);
        rbTwo = findViewById(R.id.rb_two);
        rbThree = findViewById(R.id.rb_three);
        rbFour = findViewById(R.id.rb_four);

        rg.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_one:
                setTabSelect(0);
                break;
            case R.id.rb_two:
                setTabSelect(1);
                break;
            case R.id.rb_three:
                setTabSelect(2);
                break;
            case R.id.rb_four:
                setTabSelect(3);
                break;
        }
    }

    /**
     * 隐藏Fragment,防止重叠
     *
     * @param i
     */
    private void setTabSelect(int i) {
        // 开启一个fragment 事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 县隐藏掉所有的fragment 防止重叠
        hideFragments(transaction);
        switch (i) {
            case 0:
                if (fragmentOne == null) {
                    fragmentOne = new FragmentTOne();
                    transaction.add(R.id.rl_main, fragmentOne);
                } else {
                    transaction.show(fragmentOne);
                }
                break;
            case 1:
                if (fragmentTwo == null) {
                    fragmentTwo = new FragmentTTwo();
                    transaction.add(R.id.rl_main, fragmentTwo);
                } else {
                    transaction.show(fragmentTwo);
                }
                break;
            case 2:
                if (fragmentThree == null) {
                    fragmentThree = new FragmentTThree();
                    transaction.add(R.id.rl_main, fragmentThree);
                } else {
                    transaction.show(fragmentThree);
                }
                break;
            case 3:
                if (fragmentFour == null) {
                    fragmentFour = new FragmentTFour();
                    transaction.add(R.id.rl_main, fragmentFour);
                } else {
                    transaction.show(fragmentFour);
                }
                break;
        }

        transaction.commitAllowingStateLoss();
    }

    /**
     * 隐藏所有Fragment
     *
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (fragmentOne != null) {
            if (!fragmentOne.isHidden()) {
                transaction.hide(fragmentOne);
            }
        }

        if (fragmentTwo != null) {
            if (!fragmentTwo.isHidden()) {
                transaction.hide(fragmentTwo);
            }
        }

        if (fragmentThree != null) {
            if (!fragmentThree.isHidden()) {
                transaction.hide(fragmentThree);
            }
        }

        if (fragmentFour != null) {
            if (!fragmentFour.isHidden()) {
                transaction.hide(fragmentFour);
            }
        }

    }

}
